package com.cloud.wms.ui.sort.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.cloud.core.config.ScanConfig;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarActivity;
import com.cloud.wms.base.BaseFragmentActivity;
import com.cloud.wms.base.BaseViewPagerFragment;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.main.entity.TaskEntity;
import com.cloud.wms.ui.sort.contract.OrderSortContract;
import com.cloud.wms.ui.sort.entity.BindInfoEntity;
import com.cloud.wms.ui.sort.fragment.OrderSortDetailFragment;
import com.cloud.wms.ui.sort.fragment.OrderSortFragment;
import com.cloud.wms.ui.sort.presenter.OrderSortPresenter;
import com.google.android.material.tabs.TabLayout;
import com.huawei.hms.hmsscankit.ScanUtil;
import com.huawei.hms.ml.scan.HmsScan;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @Author:Chen
 * @Date: 2021-03-07 16:09
 * @Description:
 */
public class OrderSortActivity extends BaseFragmentActivity<OrderSortPresenter> implements OrderSortContract.View {
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.vp_content)
    ViewPager vpContent;
    private BindInfoEntity bindInfoEntity;
    private TaskEntity taskEntity;

    @Override
    protected String barTitle() {
        return "订单拣选";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_common_tab;
    }

    @Override
    protected void setupComponent() {

    }

    @Override
    protected void initEvent() {
        bindInfoEntity = getIntent().getParcelableExtra(IntentKey.OBJECT_KEY);
        taskEntity = getIntent().getParcelableExtra(IntentKey.TASK_KEY);
        super.initEvent();
    }

    @Override
    protected String[] getTabs() {
        return new String[]{"拣选中", "待拣选", "已拣选"};
    }

    @Override
    protected void initFragment() {
        Bundle bundle = new Bundle();
        bundle.putParcelable("SortInfoEntity", bindInfoEntity);
        bundle.putParcelable("Task", taskEntity);
        //添加tab
        for (int i = 0; i < tabs.length; i++) {
            tabLayout.addTab(tabLayout.newTab().setText(tabs[i]));
        }
        tabFragmentList.add(OrderSortFragment.newInstance(bundle));
        Bundle bundleOrderDetail = new Bundle();
        bundleOrderDetail.putParcelable("SortInfoEntity", bindInfoEntity);
        bundleOrderDetail.putBoolean("IsComplete", false);
        bundleOrderDetail.putParcelable("Task", taskEntity);
        tabFragmentList.add(OrderSortDetailFragment.newInstance(bundleOrderDetail));
        bundle.putBoolean("IsComplete", true);
        tabFragmentList.add(OrderSortDetailFragment.newInstance(bundle));
    }

    @Override
    public void onSuccess(Message message) {
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ScanConfig.SCAN_CAMERA_REQUEST_CODE && resultCode == RESULT_OK && data != null) {
            /**
             * 扫码返回的数据（码格式，扫码类型，扫码内容）
             * obj.getOriginalValue();//扫码的结果
             * obj.getScanType();//二维码的格式
             * obj.getScanTypeForm();//二维码内容的类型
             */
            HmsScan obj = data.getParcelableExtra(ScanUtil.RESULT);
            if (obj != null && !TextUtils.isEmpty(obj.getOriginalValue())) {
                tabFragmentList.get(0).onActivityResult(requestCode, resultCode, data);
            } else {
                info("扫描结果解析失败！");
            }
        }
    }

    public static void start(Context context, BindInfoEntity entity, TaskEntity taskEntity) {
        Intent intent = new Intent(context, OrderSortActivity.class);
        intent.putExtra(IntentKey.OBJECT_KEY, entity);
        intent.putExtra(IntentKey.TASK_KEY, taskEntity);
        context.startActivity(intent);
    }
}
